<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>multi points</title>
</head>

<body onload="main()">
    <canvas id="webGl" width="400" height="400">
        Please use a browser that supports 'canvas'
    </canvas>
    <script src="../lib/webgl-utils.js"></script>
    <script src="../lib/webgl-debug.js"></script>
    <script src="../lib/cuon-utils.js"></script>
    <script>
        // 顶点着色器程序
        var VSHADER_SOURCE =
            'attribute vec4 a_Position;\n' +
            'attribute float a_PointSize;\n' +
            'void main() {\n' +
            ' gl_Position = a_Position;\n' + // 设置坐标
            ' gl_PointSize = a_PointSize;\n' + // 设置尺寸
            '}\n';
        // 片元着色器程序
        var FSHADER_SOURCE =
            'precision mediump float;\n' +  // 精度声明
            'uniform vec4 u_FragColor;\n' +
            'void main() {\n' +
            ' gl_FragColor = u_FragColor;\n' + // 设置颜色
            '}\n';

        function main() {
            var canvas = document.getElementById('webGl');

            var gl = getWebGLContext(canvas);

            if (!gl) {
                console.log('Failed to get the rending context for WebGL!');
                return;
            }

            // 初始化着色器
            if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
                console.log('Failed to initialize shaders.');
                return;
            }

            // 获取attribute变量的存储位置
            var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor');

            if (!u_FragColor) {
                console.log('Failed to get u_FragColor variable');
                return;
            }

            // 设置顶点位置
            var n = initVertexBuffers(gl);

            if (n < 0) {
                console.log('Failed to set the positions of the vertices');
                return;
            }

            gl.uniform4f(u_FragColor, 1.0, 0.0, 1.0, 1.0);

            // 设置<canvas>的背景色
            gl.clearColor(0.0, 0.0, 0.0, 1.0);

            // 清空canvas
            gl.clear(gl.COLOR_BUFFER_BIT);

            // 绘制n个点
            gl.drawArrays(gl.POINT, 0, n);
        }

        function initVertexBuffers(gl) {
            var vertices = new Float32Array([
                0.0, 0.5, -0.5, -0.5, 0.5, -0.5
            ]);
            var sizes = new Float32Array([
                10.0, 15.0, 20.0
            ]);
            var n = 3;//点的个数

            // 创建缓冲区对象
            var vertexBuffer = gl.createBuffer(),
                sizeBuffer = gl.createBuffer(),
                a_PointSize = gl.getAttribLocation(gl.program, 'a_PointSize'),
                a_Position = gl.getAttribLocation(gl.program, 'a_Position');

            if (!vertexBuffer || !sizeBuffer) {
                console.log('Failed to create the buffer object');
                return -1;
            }

            if (a_Position < 0 || a_PointSize < 0) {
                console.log('Failed to get the storage location of a_Position/a_PointSize');
                return -1;
            }

            // 将缓冲区对象绑定到目标
            gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
            // 向缓冲区对象写入数据
            gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
            // 将缓冲区对象分配给a_Position变量
            gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
            // 连接a_Position变量与分配给它的缓冲区对象
            gl.enableVertexAttribArray(a_Position);

            gl.bindBuffer(gl.ARRAY_BUFFER, sizeBuffer);
            gl.bufferData(gl.ARRAY_BUFFER, sizes, gl.STATIC_DRAW);
            gl.vertexAttribPointer(a_PointSize, 1, gl.FLOAT, false, 0, 0);
            gl.enableVertexAttribArray(a_PointSize);

            return n;
        }
    </script>
</body>

</html>